package com.yequan.thread.cyclicbarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author yq
 * @date 2020/09/03
 */
public class CyclicBarrierDemo3 {

    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
        @Override
        public void run() {
            System.out.println("线程：" + Thread.currentThread().getName() + "执行任务");
        }
    });

    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        new Thread(() -> {
            try {
                System.out.println("线程："+Thread.currentThread().getName()+"到达屏障");
                cyclicBarrier.await();
                System.out.println("线程：" + Thread.currentThread().getName() + "1111111");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }).start();
        cyclicBarrier.await();
        System.out.println("线程："+Thread.currentThread().getName()+"到达屏障");
        System.out.println("线程：" + Thread.currentThread().getName() + "222222");
    }

}
